package com.myStoreorderProvider.config;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.myStoreorderProvider.entity.AlipayBean;
import org.springframework.stereotype.Component;

/**
 * @author oahner
 * @create 2022-04-26 18:52
 */
@Component
public class Alipay {


    /**
     * 支付接口
     * @param alipayBean
     * @return
     * @throws AlipayApiException
     */
    public static String pay(AlipayBean alipayBean) throws AlipayApiException {
        // 1、获得初始化的AlipayClient
     /*   String serverUrl = AlipayProperties.getGatewayUrl();
        String appId = AlipayProperties.getAppId();
        String privateKey = AlipayProperties.getPrivateKey();
        String format = "json";
        String charset = AlipayProperties.getCharset();
        String alipayPublicKey = AlipayProperties.getPublicKey();
        String signType = AlipayProperties.getSignType();
        String returnUrl = AlipayProperties.getReturnUrl();
        String notifyUrl = AlipayProperties.getNotifyUrl();*/

        String serverUrl = "https://openapi.alipaydev.com/gateway.do";
        String appId = "2021000119622772";
        String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCYdEyvXQgYB+FVsxs4SO2iBLlUgHJ6BE9EJEgG2b0qjpAPvnmUOV2GutnK8iP5Ey0IJOR3IkJ7KcaQp1dx83t5JQOx6nr8wpVUburaA6x+O9JkEVjtbHwPe56UWlhI5SnWwcafPqeKq0XDtOcKP98L4r9t1s8kJ9l/IdUBiKz49BN+TTb+V55L2f5xvV4RRlugyOnXvHfXDp9jsyhtwrymQPU0VNlAI1W4aTwmL+3ws/sDa8gC+BbglnhPb2aw+ls8+vKbJOxjWE622ZaJUYM4fsx50BkSn/vdHgnonjm4vQPsgzB53jlAxM/bIWHRbNDBjafiGY3hcqAhl5Ev/Ib9AgMBAAECggEASdijwJn+rq06gCmWuMfSm5VQNFdXnDT0NFyiqvbTg4a0z3uDJsI2xGlDz+NEe4THXleIdxCYRVJyBWWZnO/E0mSUipy64C5c6R4lEl+P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8NxpZZyX2fUia45WUx75ZdQ7XV2SzPXI=";
        String format = "json";
        String charset = "utf-8";
        String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjYjMLKJMn+z99kG81v8dErLJgEmt78YKpJPBTjlKMNfxNsATU0i223a3Xh8av87XhfoN12X+B4Q+nTsEjOcSE/Im0OQ39X/6wxiW31qzsHe44USFmCO2eMeJwtL0mRFjKIeL8DDhHwvtfgozyiq8WmyXI+k+dHWuRERnMd7YtM/MQmz4atIEMaQEv7Iuzc/QMnFawF8drRjF6023dGsea6DOgMgtr/a3iflYN93o6sOAsVetrU86kK5UoKy8OlQvFIpGuaaeGKjp421mPmhT10YexWI+ANfqQN6QBPMRam0N2hmZBpR2UK9c/hjVzu+oYFNRljYc2CuDir1xDzxW5QIDAQAB";
        String signType = "RSA2";
        String notifyUrl = "https://6ae9-175-7-125-155.jp.ngrok.io:10006/alipay/Notify";
        String returnUrl = "http://192.168.0.15:10006/alipay/pay_return";
//        String log_path="C://";



        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, appId, privateKey, format, charset, alipayPublicKey, signType);
        // 2、设置请求参数
        AlipayTradePagePayRequest alipayTradePagePayRequest = new AlipayTradePagePayRequest();
        // 页面跳转同步通知页面路径
        alipayTradePagePayRequest.setReturnUrl(returnUrl);
        // 服务器异步通知页面路径
        alipayTradePagePayRequest.setNotifyUrl(notifyUrl);
        // 封装参数
        alipayTradePagePayRequest.setBizContent(JSON.toJSONString(alipayBean));
        // 3、请求支付宝进行付款，并获取支付结果
        String result = alipayClient.pageExecute(alipayTradePagePayRequest).getBody();
        // 返回付款信息
        return result;
    }

}
